package operation;

import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: XMQ
 * Date: 2022-05-26
 * Time: 9:11
 */
public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        System.out.println("请输入要删除书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int index = 0;
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            if(name.equals(bookList.getBook(i).getName())){
                index = i;
                break;
            }else{
                index = -1;
            }
        }
        if(index == -1){
            System.out.println("没有这本书");
            return;
        }

        for (int i = index; i < bookList.getUsedSize()-1; i++) {
            bookList.setBook(i,bookList.getBook(i+1));
        }
        bookList.setUsedSize(-1);
        System.out.println("删除成功！");
    }
}
